function initQuery(data) {
    let query = '?'
    const keys = Object.keys(data)
    const len = keys.length
    for(let i = 0; i < len; i++) {
        if (i > 0) {
            query += `&${keys[i]}=${data[keys[i]]}`
        } else {
            query += `${keys[i]}=${data[keys[i]]}`
        }
    }
    return query === '?' ? '': query
}

class Navigate {
    to(url, data = {}, events) {
        let query = initQuery(data)
        uni.navigateTo({
            url: url + query,
            events: events,
        })
    }
    back(data = {}) {
        uni.navigateBack(data)
    }
    switchTab(url) {
        uni.switchTab({
            url
        })
    }
    redirectTo(url, data = {}) {
        let query = initQuery(data)
        console.log(url + query)
        uni.redirectTo({
        	url: url + query,
            success(e) {
                console.log(e)
            },
            fail(err) {
                console.log(err)
            }
        });
    }
    reLaunch(url) {
      uni.reLaunch({
        url
      })
    }
}

export default new Navigate()